﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ODM_Code.Forms
{
    public partial class ConstraintsForm : Form
    {
        private Code.ConstraintsCode cc;
        private string dbfPath;
        private string inputPath;
        private string alternativePath;
        private int totCount = 0;
        bool newRow = false;

        public ConstraintsForm()
        {
            InitializeComponent();

            dbfPath = "C:\\magfire\\" + ConfigSettings.ReadSetting("analysisArea");
            inputPath = dbfPath + "\\input";
            alternativePath = inputPath + "\\" + ConfigSettings.ReadSetting("currentAlternative");
            cc = new Code.ConstraintsCode(dbfPath, inputPath, alternativePath);
        }

        private void ConstraintsForm_Load(object sender, EventArgs e)
        {
            int fireCount = Code.ConstraintsCode.fireCount;
            int periodCount = Code.ConstraintsCode.periodCount;
            totCount = fireCount * periodCount;
            cc.ParseConstraintFile();

            List<string[]> rowsToAdd = cc.getConstraintRows();
            foreach (string[] s in rowsToAdd)
            {
                dataGridView1.Rows.Add(s);
            }

            this.RowLocker();
        }

        private void RowLocker()
        {
            if (dataGridView1.Rows.Count <= totCount + 1)
            {
                dataGridView1.AllowUserToDeleteRows = false;
            }
            else
            {
                dataGridView1.AllowUserToDeleteRows = true;
            }
        }

        private void butDone_Click(object sender, EventArgs e)
        {
            cc.WriteConstraints();
            this.Close();
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            this.RowLocker();
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            this.RowLocker();
        }

        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            newRow = true;
            this.RowLocker();
        }

        private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
            this.RowLocker();
        }

        private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            this.RowLocker();
        }

        private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (newRow == true)
            {
                int newRowId = dataGridView1.Rows.GetLastRow(DataGridViewElementStates.Visible);
                DataGridViewRow row = dataGridView1.Rows[newRowId];
                object[] vals = new object[6];
                for (int i = 0; i < vals.Length; i++)
                {
                    vals[i] = row.Cells[i].Value;
                }
                cc.AddConstraint(vals);
            }

            newRow = false;
            this.RowLocker();
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int colId = dataGridView1.CurrentCell.ColumnIndex;
            int rowId = dataGridView1.CurrentCell.RowIndex;
            object val = dataGridView1.CurrentCell.Value;
            cc.UpdateConstraint(colId, rowId, val);
        }

        
        
    }
}
